package com.sohu.tv.utils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieTool {
	/**
	  * 设置cookie（接口方法）
	  * @author YunLong
	  * @param response
	  * @param name  cookie名字
	  * @param value cookie值
	  * @param maxAge cookie生命周期  以秒为单位
	  */
	  public static void addCookie(HttpServletResponse response,String name,String value,int maxAge){
	    Cookie cookie = new Cookie(name,value);
	    if(maxAge>0){  
	      cookie.setMaxAge(maxAge);
	    }
	  //cookie有效路径是网站根目录
	    cookie.setPath("/");
	    response.addCookie(cookie);
	    }
	  
	  
	  /**
	  * 将cookie封装到Map里面（非接口方法）
	  * author YunLong
	  * param request
	  * return
	  */
	  public static Map<String,String> ReadCookieMap(HttpServletRequest request){ 
	  Map<String,String> cookieMap = new HashMap<String,String>();
	  Cookie[] cookies = request.getCookies();
	  if(null!=cookies){
	    for(Cookie cookie : cookies){
	     cookieMap.put(cookie.getName(), cookie.getValue());
	    }
	  }
	  return cookieMap;
	  }
	  
}
